package com.viewshine.webservicedemo.config;

import com.viewshine.webservicedemo.service.TestService;
import com.viewshine.webservicedemo.service.TestServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * @ClassName CxfConfig
 * @Author rhb
 * @Description //TODO
 * @Date 2019-01-16 14:43
 * @Version 1.0
 **/
@Configuration
public class CxfConfig {

    @Autowired
    private TestService testService;

    /**
     * 此方法作用是改变项目中服务名的前缀名，此处127.0.0.1或者localhost不能访问时，请使用ipconfig查看本机ip来访问
     * 此方法被注释后:wsdl访问地址为http://127.0.0.1:8080/services/user?wsdl
     * 去掉注释后：wsdl访问地址为：http://127.0.0.1:8080/soap/user?wsdl
     * 【不要将CXFCXFServlet拦截器 命名为dispatcherServlet 这样会和SpringBoot默认的DispatcherServlet（MVC）拦截器Bean注入冲突】
     * @return
     */
    @SuppressWarnings("all")
    @Bean
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }


    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), testService);
        endpoint.publish("/user");
        return endpoint;
    }


}
